package model

import (
    "time"
    "{{.projectName}}/variable"
)
{{if eq .isUser true}}
type UserLoginInfo struct {
	Username  string    `json:"username"`
	Password  string    `json:"password"`
}
{{- end}}

type {{.structName}} struct{
    Id {{.pkType}}
    {{- range .fieldNameList}}
    {{.FieldName}} {{.FieldType}}
    {{- end}}
    CreatedAt time.Time
    UpdatedAt time.Time
}

func New{{.structName}}() *{{.structName}} {
	return &{{.structName}}{}
}

{{- if eq .isUser true}}
func ({{.objectName}} *{{.structName}}) Login(o *{{.structName}}, uli *UserLoginInfo) {
	variable.DB.Where("username = ? and password = ?", uli.Username, uli.Password).First(&o)
}
{{- end}}

func ({{.objectName}} *{{.structName}}) Created{{.structName}}(o *{{.structName}}) map[string]interface{} {
    variable.DB.Create(o)

    return map[string]interface{}{"code":"200", "data":nil, "message":"成功"}
}

func ({{.objectName}} *{{.structName}}) Update{{.structName}}(o *{{.structName}}) map[string]interface{} {
    variable.DB.Create(o)

    return map[string]interface{}{"code":"200", "data":nil, "message":"成功"}
}

func ({{.objectName}} *{{.structName}}) Delete{{.structName}}(id {{.pkType}}) map[string]interface{} {
    variable.DB.Delete(&{{.structName}}{}, id)

    return map[string]interface{}{"code":"200", "data":nil, "message":"成功"}
}

func ({{.objectName}} *{{.structName}}) Find{{.structName}}All(list []*{{.structName}}, condition map[string]interface{}, pageSize int, pageNo int) {
    variable.DB.Limit(pageSize).
    Offset((pageNo-1)*pageSize).
    Where(condition).
    Find(&list)
}

func ({{.objectName}} *{{.structName}}) Find{{.structName}}One(o *{{.structName}}, id {{.pkType}}) {
    variable.DB.Where("id = ?", id).First(o)
}
